﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bibilioteca.Dominio;
using Bibilioteca.Models;

namespace Bibilioteca.Controllers
{
    public class LoginController : Controller
    {
        NegocioLogin negocioLogin = new NegocioLogin();

        //
        // GET: /Login/

        public ActionResult Index()
        {
            return RedirectToAction("Login");
        }

        public ActionResult Login()
        {
            if (negocioLogin.permisosSocio(Session))
            {
                String[] s = negocioLogin.redireccion(Session);
                return RedirectToAction(s[0], s[1]);
            } 
            return View();
        }

        //
        // POST: /Login/Login

        [HttpPost]
        public ActionResult Login(FormCollection collection)
        {
            try
            {
                NegocioLogin negLogin = new NegocioLogin();
                
                string usuario = collection["UserName"];
                string password = collection["Password"];

               // Socio s = socios.GetSocioConUsername(usuario);
                Socio s = negLogin.validarUsuario(usuario, password);
                if (s.Id > 0)
                {
                    Session["user"] = usuario;
                    Session["esAdmin"] = negLogin.esAdmin(s);
                    //Usuario logueado correctamente

                    String[] str = negocioLogin.redireccion(Session);
                    return RedirectToAction(str[0], str[1]);
                }

                //return RedirectToAction("Index");
                return View();
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Logout()
        {
            Session.Abandon();
            return RedirectToAction("Login");
        }

        
    }
}
